﻿using System;
using System.Data;
using System.Transactions;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;
using Skyworth.CRM.Common;

namespace Skyworth.CRM.DAL
{
    public class ProductAttachmentDAL : SuperDataBase, IProductAttachmentDAL
    {        
		#region  Method

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public void Add(ProductAttachment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_BasicData_ProductAttach(");
			strSql.Append("AttachId,ProductId,AttachName,AttachFile,Description)");
			strSql.Append(" values (");
			strSql.Append("@AttachId,@ProductId,@AttachName,@AttachFile,@Description)");
            DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
            DB.AddInParameter(cmd, "@AttachId", DbType.Int32, model.AttachId);
            DB.AddInParameter(cmd, "@ProductId", DbType.Int32, model.ProductId);
            DB.AddInParameter(cmd, "@AttachName", DbType.String, model.AttachName);
            DB.AddInParameter(cmd, "@AttachFile", DbType.String, model.AttachFile);
            DB.AddInParameter(cmd, "@Description", DbType.String, model.Description);
            DB.ExecuteNonQuery(cmd);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int AttachId)
		{			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_BasicData_ProductAttach ");
			strSql.Append(" where AttachId=@AttachId ");
            DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
            DB.AddInParameter(cmd, "@AttachId", DbType.Int32, AttachId);
            int rows = DB.ExecuteNonQuery(cmd);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public ProductAttachment GetModel(int AttachId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AttachId,ProductId,AttachName,AttachFile,Description from T_BasicData_ProductAttach ");
			strSql.Append(" where AttachId=@AttachId ");
            DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
            DB.AddInParameter(cmd, "@AttachId", DbType.Int32, AttachId);
            ProductAttachment model = new ProductAttachment();
            DataSet ds = DB.ExecuteDataSet(cmd);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["AttachId"].ToString()!="")
				{
					model.AttachId=int.Parse(ds.Tables[0].Rows[0]["AttachId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ProductId"].ToString()!="")
				{
					model.ProductId=int.Parse(ds.Tables[0].Rows[0]["ProductId"].ToString());
				}
				model.AttachName=ds.Tables[0].Rows[0]["AttachName"].ToString();
				model.AttachFile=ds.Tables[0].Rows[0]["AttachFile"].ToString();
				model.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				return model;
			}
			else
			{
				return null;
			}
		}

		#endregion  Method
    }
}